﻿using System.Collections;
using UnityEngine;
using DG.Tweening;

namespace ZMS
{
    public class 单条铝锭生产线 : ProductLinesMono
    {
        //public Animator animator_10kg堆垛机;
        public 堆垛机10kg_Contor 堆垛机10kg_Contor;
        public int 铝锭Index = 0;

        public static 单条铝锭生产线 instances;
        public Transform ingotParent;//铝锭父级
        public IngotItemLine ingotItem;//铝锭
        #region 铝锭所在位
        public BoxCollider belt_1;//一节传送带
        public BoxCollider moveSpeed_2;//速度二
        public BoxCollider ingotIns;//生成位
        public BoxCollider moveSpeed_3;//生成位
        public BoxCollider correct_1;//矫正位设备_1
        public BoxCollider correct_2;//矫正位设备_2
        public BoxCollider flip;//翻转位设备
        public bool flipIs = false;
        public BoxCollider stop;//停止位

        Vector3 leftCorrect_V3 = new Vector3(0, -6.5f, -90);//左矫正旋转角度
        Vector3 rightCorrect_V3 = new Vector3(0, 6.4f, -90);//右矫正旋转角度
        Vector3 correctStart_V3 = new Vector3(0, 0, -90);//矫正初始角度
        #endregion

        #region 设备
        public Transform rectifyLeft_1;//矫正夹_左一
        public Transform rectifyRight_1;//矫正夹_右一

        public Transform rectifyLeft_2;//矫正夹_左二
        public Transform rectifyRight_2;//矫正夹_右二

        public Transform flipTf;//翻转设备

        public GameObject ingotArr;
        internal Transform ingotArr_ins;//铝锭层集合
        public Transform ingotArrIns_parent;//铝锭层于机器人父级
        public Transform ingotArr_pile;
        internal Transform ingotArr_pile_ins;
        #endregion

        private void Awake()
        {
            instances = this;
        }

        void Start()
        {
            for (int i = 0; i < linesAnimations.Count; i++)//不播放打包
            {
                Debug.Log(linesAnimations[i].name);
                Debug.Log(linesAnimations[i].GetClipCount());
                if (i == linesAnimations.Count - 1)
                {
                    AnimationsManager.Ongoing(linesAnimations[i], linesAnimations[i].name, 0.2f);
                }
                else { }
                linesAnimations[i].Play(linesAnimations[i].name);

                //linesAnimations[i].GetClip(linesAnimations[i].name).AddEvent(new AnimationEvent() {time = 2f,functionName = "PrintEvent", intParameter  = 9});
            }
            InsIngots();

            堆垛机10kg_Contor.switch_10kg堆垛机 += SetIngotArrParent;
            堆垛机10kg_Contor.contor_V(ingotArr_pile_ins.childCount);

            ingotArr_pile_ins = GameObject.Instantiate(ingotArr_pile);
            DaBao_Contor.instances.switchAin += ingotArr_pile_ins.GetComponent<IngotPile>().SwitchSpeed;
        }

        private void FixedUpdate()
        {
            //transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
            if (Input.GetKeyUp(KeyCode.J))
            {
                //Rectify(rectifyLeft);
                //Rectify(rectifyRight);

                //flipTf.DOLocalRotate(new Vector3(0, flipTf.localEulerAngles.y + 180, 0), 2).SetEase(Ease.Linear);
                //flipTf.DORotateQuaternion(Quaternion.Euler(0, 180, 0), 1).SetEase(Ease.Linear);
                MotionManager.instance.RotationObj(flipTf);
            }
        }

        /// <summary>
        /// 实例化铝锭至传送带
        /// </summary>
        public void InsIngots()
        {
            IngotItemLine ingotItemLine = Instantiate<IngotItemLine>(ingotItem, ingotParent);
            ingotItemLine.indexIngot = ingotParent.childCount;
        }

        /// <summary>
        /// 矫正动作
        /// </summary>
        /// <param name="rectify"></param>
        private Tween RectifyMovement(Transform rectify)
        {
            if (rectify == rectifyLeft_1 || rectify == rectifyLeft_2)
                return rectify.DOLocalRotate(leftCorrect_V3, 0.7f).SetEase(Ease.InOutElastic);
            else
                return rectify.DOLocalRotate(rightCorrect_V3, 0.7f).SetEase(Ease.InOutElastic);
        }

        public void Rectify(Transform rectify, IngotItemLine ingotItemLine = null)
        {
            StartCoroutine(Rectify_IE(rectify, ingotItemLine));
        }

        IEnumerator Rectify_IE(Transform rectify, IngotItemLine ingotItemLine)
        {
            yield return null;
            Tween tween = RectifyMovement(rectify);
            yield return tween.WaitForCompletion();//待当前DOTween执行完毕
            yield return new WaitForSeconds(0.3f);
            if (ingotItemLine != null)
                ingotItemLine.speedMove = 0.08f;
            rectify.DOLocalRotate(correctStart_V3, 0.7f).SetEase(Ease.InOutElastic);
        }

        public void IngotArr_AddIngotIns(Transform ingotIns)
        {
            if (ingotArr_ins == null)
            {
                ingotArr_ins = GameObject.Instantiate(ingotArr.transform);
            }
            if (ingotArr_pile_ins == null)
            {
                ingotArr_pile_ins = GameObject.Instantiate(ingotArr_pile);
                DaBao_Contor.instances.switchAin += ingotArr_pile_ins.GetComponent<IngotPile>().SwitchSpeed;
            }
            if (ingotArr_pile_ins.childCount == 0 && ingotArr_ins.childCount < 6)
            {
                flipIs = false;
            }
            else
                flipIs = true;

            //ingotIns.SetParent(ingotArr_ins.transform);
            //Debug.Log("ingotArr_pile_ins" + ingotArr_pile_ins.childCount);
            //Debug.Log("ingotArr_ins" + ingotArr_ins.childCount);
            //if (ingotArr_pile_ins.childCount == 0 && ingotArr_ins.childCount == 6)
            //{
            //    堆垛机10kg_Contor.contor_V(ingotArr_pile_ins.childCount);
            //    return;
            //    //抓取

            //}
            //if (ingotArr_ins.childCount == 7)
            //{
            //    堆垛机10kg_Contor.contor_V(ingotArr_pile_ins.GetChildCount());
            //    //抓取
            //    //animator_10kg堆垛机.SetInteger("play",0);

            //}
        }

        //抓取
        public void Grab(Transform ingotIns)
        {
            ingotIns.SetParent(ingotArr_ins.transform);
            Debug.Log("ingotArr_pile_ins" + ingotArr_pile_ins.childCount);
            Debug.Log("ingotArr_ins" + ingotArr_ins.childCount);
            if (ingotArr_pile_ins.childCount == 0 && ingotArr_ins.childCount == 6)
            {
                堆垛机10kg_Contor.contor_V(ingotArr_pile_ins.childCount);
                return;
                //抓取

            }
            if (ingotArr_ins.childCount == 7)
            {
                堆垛机10kg_Contor.contor_V(ingotArr_pile_ins.childCount);
                //抓取
                //animator_10kg堆垛机.SetInteger("play",0);

            }
        }

        public void SetIngotArrParent(int parent_int)
        {
            Debug.Log(parent_int);
            if (parent_int == 0)
            {
                ingotArr_ins.SetParent(ingotArrIns_parent);
            }
            else
            {
                AddIngotArr_pile();
            }
        }

        public void AddIngotArr_pile() //添加至垛
        {
            ingotArr_ins.SetParent(ingotArr_pile_ins);//将铝锭添加至垛

            
            if (ingotArr_pile_ins.childCount != 0&& !flipIs)
            {
                flipIs = true;
            }
            else if (ingotArr_pile_ins.childCount == 1)
            {
                flipIs = false;
            }

            Debug.Log(ingotArr_ins.childCount);
            for (int i = 0; i < ingotArr_ins.childCount; i++)
            {
                ingotArr_ins.transform.GetChild(i).GetComponent<Rigidbody>().isKinematic = false;
            }
            StartCoroutine(enumerator());

        }

        IEnumerator enumerator()
        {
            yield return new WaitForSeconds(1f);
            for (int i = 0; i < ingotArr_ins.childCount; i++)
            {
                ingotArr_ins.transform.GetChild(i).GetComponent<Rigidbody>().isKinematic = true;
            }

            ingotArr_ins = null;
            Debug.Log("aa" + ingotArr_pile_ins.childCount);
            if (ingotArr_pile_ins.childCount == 15)
            {
                DaBao_Contor.instances.StartPlay();
                ingotArr_pile_ins = null;
            }
        }
    }
}